const { ccclass, property } = cc._decorator;
import MonsterControl from './MonsterControl';
import MyEnums from '../MyEnums';
import AttackManager from '../SkillAttack/AttackManager';
import TargetBase from '../SkillAttack/TargetBase';

@ccclass
export default class SkillBlast extends cc.Component {
    @property(cc.String)
    skillName: string = "01";
    @property
    area: number = 60;
    @property
    addDamage: number = 0;
    @property
    sample: number = 0;
    @property([cc.SpriteFrame])
    StatusSprite: cc.SpriteFrame[] = [];

    @property(cc.CircleCollider)
    _Collider: cc.CircleCollider = null;
    @property
    _MonsteRoot: cc.Node = null;
    @property
    _MonsterControl: MonsterControl = null;
    @property(cc.Node)
    _AttacksNode: cc.Node = null;
    @property
    _sprite: cc.Sprite = null;
    @property
    _anim: cc.Animation = null;
    @property
    _controlAttack: AttackManager = null;

    Attacked: string[] = [];//已经输出过伤害的节点ID，防重复输出
    _isPleying: boolean = false;
    _doAttack: boolean = false;
    onLoad() {
        if (this._anim) {
            this._anim.on('finished', this.finishedAnim, this);
        }
    }
    init() {
        this._MonsterControl = this._MonsteRoot.getComponent(MonsterControl);
        this.BuildAttack();
        this.initStatus();
    }
    finishedAnim() {
        this._isPleying = false;
        this._doAttack = false;
        let sprite = this.node.getComponent(cc.Sprite);
        sprite.spriteFrame = null;
    }
    /**
     * 伤害输出（爆体）
     * @param target 
     */
    doAttack(target: cc.Node) {
        this._controlAttack.damageAttack = this._MonsterControl.attackDamage;
        let _TargetBase = target.parent.parent.getComponent(TargetBase);
        if (!this._isPleying) {
            if (this._MonsterControl.doAttack()) {
                this._controlAttack.doAttack(target, MyEnums.SkillSource.hero, _TargetBase);
                this._anim.play("anim_Status");
                this._isPleying = true;
                return true;
            }
        }
        return false;
    }
    onCollisionEnter(other, self) {
        if (!this._doAttack) {
            this.checkCollision(other);
        }
    }
    onCollisionStay(other, self) {
        if (!this._doAttack) {
            this.checkCollision(other);
        }
    }
    checkCollision(other) {
        if (this.Attacked.indexOf(other.node.uuid) > -1)
            return;
        if (other.tag == TagHeroBody) {
            if (this._MonsterControl._Status == MyEnums.MonsterStatus.await) {
                this._doAttack = this.doAttack(other.node);
            }
        }
    }
    private BuildAttack() {
        this._isPleying = false;
        this._doAttack = false;
        this._AttacksNode = new cc.Node();
        this._controlAttack = this._AttacksNode.addComponent(AttackManager);
        this._controlAttack.addRefresItem(MyEnums.SkillRefreshType.life, this.addDamage);
        this._controlAttack.attribute = this._MonsterControl.attribute;
        this._controlAttack.source = MyEnums.SkillSource.monster;
        this.node.addChild(this._AttacksNode);
    }
    private initStatus() {
        if (this.area > 0) {
            let _Collider = this.node.addComponent(cc.CircleCollider);
            _Collider.radius = this.area;
        }
        this._sprite = this.node.addComponent(cc.Sprite);
        if (this.StatusSprite?.length > 1) {
            if (this._anim == null) {
                this._anim = this.node.addComponent(cc.Animation);
                var clip = cc.AnimationClip.createWithSpriteFrames(this.StatusSprite, this.StatusSprite.length * this.sample);
                clip.name = "anim_Status";
                this._anim.addClip(clip);
            }
        }
    }
}
